package airthmetic.exercise.linkedList;

public class _24_两两交换链表中的节点 {
    public static ListNode swapPairs(ListNode head) {
        if (head == null){
            return null;
        }
        if (head.next == null){
            return head;
        }
        ListNode prev = new ListNode(-1);
        ListNode current = head;
        prev.next = current;
        ListNode res = prev;
        while (current != null && current.next != null){
            ListNode next = current.next;
            ListNode next_next = next.next;

            next.next = current;
            current.next = next_next;
            prev.next = next;

            prev = current;
            current = next_next;
        }
        return res.next;
    }

    public static void main(String[] args) {
        ListNode listNode4 = new ListNode(4, null);
        ListNode listNode3 = new ListNode(3, listNode4);
        ListNode listNode2 = new ListNode(2, listNode3);
        ListNode listNode1 = new ListNode(1, listNode2);


        ListNode listNode = swapPairs(listNode1);
        System.out.println(listNode);
    }
}
